package com.ruin.tech.ahri.service.impl;

import com.ruin.tech.ahri.exception.BizException;
import com.ruin.tech.ahri.service.IMailService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;

/**
 * @program ahri
 * @description: 邮箱服务
 * @author: ruin
 * @create: 2019/12/24 16:27
 */
@Slf4j
@Service
public class MailServiceImpl implements IMailService {
    @Autowired
    private JavaMailSender mailSender;

    @Value("${mail.fromMail.addr}")
    private String from;

    @Override
    public void sendMail(String to, String subject, String content) throws BizException {
        SimpleMailMessage mailMessage=new SimpleMailMessage();
        //邮件发送方
        mailMessage.setFrom(from);
        //邮件接收者
        mailMessage.setTo(to);

        //如果发给多个人的：
        //mailMessage.setTo("1xx.com","2xx.com","3xx.com");    
        mailMessage.setSubject(subject);
        mailMessage.setText(content);
        try {
            mailSender.send(mailMessage);
        }catch (Exception e){
            log.error("验证码邮件发送失败,email:"+to,e);
            throw new BizException("验证码邮件发送失败");
        }
    }
}
